package com.xiaoxin.experience.util;

import com.xiaoxin.experience.common.BusinessException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

/**
 * @author xiaoxin
 */
public class IgnoredSSLSocketFactory
{

    public static SSLSocketFactory getSocketFactory()
    {
        try
        {
            TrustManager[] trustAllCerts = new TrustManager[]{getX509TrustManager()};
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            return sc.getSocketFactory();
        }
        catch (NoSuchAlgorithmException | KeyManagementException e)
        {
            throw new BusinessException(-1,"ssl socket factory create fail");
        }
    }

    public static X509TrustManager getX509TrustManager()
    {
        return new X509TrustManager()
        {
            @Override
            public X509Certificate[] getAcceptedIssuers()
            {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType)
            {}

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType)
            {}
        };
    }
}
